
package de.mssp.sensorkit.accelerometer;

import de.mssp.sensorkit.R;
import android.app.Activity;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.widget.TextView;

public class AccelerometerActivity extends Activity implements SensorEventListener
{	
    TextView textViewX;
    TextView textViewY;
    TextView textViewZ;
    TextView textViewMax;
    float max = 0;

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        
        SensorManager sensorManager = (SensorManager) getSystemService(SENSOR_SERVICE); //Get general SensorManager
        Sensor accSensor = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER).get(0);//Get Acc. Sensor
        sensorManager.registerListener(this, accSensor, SensorManager.SENSOR_DELAY_UI);//Register to listen to SensorService
        
        //Initialize TextView, to show sensor values
        setContentView(R.layout.accelerometer);
        textViewX = (TextView) findViewById(R.id.x);
        textViewY = (TextView) findViewById(R.id.y);
        textViewZ = (TextView) findViewById(R.id.z);
        textViewMax = (TextView) findViewById(R.id.max);//Maximum tracked value

    }
    
    public void onAccuracyChanged (Sensor sensor, int accuracy)
    {
    	//Ignore
    }
    
    public void onSensorChanged (SensorEvent event)
    {
    	//Gather sensor values
    	float x = Math.round(event.values[0]*100)/100;
    	float y = Math.round(event.values[1]*100)/100;
    	float z = Math.round(event.values[2]*100)/100;
    	
    	//Reset displayvalues
    	String xbar = "";
    	String ybar = "";
    	String zbar = "";
    	
    	//Check maximum
    	max = Math.max(Math.max(x, y), Math.max(z, max));

    	//Make graphs
    	for (int i=0; i<Math.abs(x) ; i++) xbar = xbar+"X";
    	for (int i=0; i<Math.abs(y); i++) ybar = ybar+"X";
    	for (int i=0; i<Math.abs(z); i++) zbar = zbar+"X";

    	//Display Values
    	textViewX.setText("X - "+Float.toString(x)+": "+xbar);
    	textViewY.setText("Y - "+Float.toString(y)+": "+ybar);
    	textViewZ.setText("Z - "+Float.toString(z)+": "+zbar);
    	textViewMax.setText("Max: "+Float.toString(max));
    }
}
